using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace SATF.BI
{
    public class FileUtil
    {

        public static List<string> ReadFileToLines(string filePath)
        {
            List<string> lines = new List<string>();
            if (!File.Exists(filePath))
            {
                return lines;
            }
            try
            {
                StreamReader reader = new StreamReader(filePath, Encoding.GetEncoding("gbk"));
                while (!reader.EndOfStream)
                {
                    try
                    {
                        lines.Add(reader.ReadLine());
                    }
                    catch (System.Exception e)
                    {

                    }
                }
                reader.Close();

                return lines;
            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.StackTrace);
                return null;
            }
 
        }

        public static string ReadFileToData(string filePath)
        {
            string content = "";
            if (!File.Exists(filePath))
            {
                return "";
            }
            try
            {
                StreamReader reader = new StreamReader(filePath, Encoding.GetEncoding("gbk"));
                content = reader.ReadToEnd();
                reader.Close();
                return content;
            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.StackTrace);
                return "";
            }
        }

        public static void WriteFile(List<string> lines, string filePath)
        {
            if (!File.Exists(filePath))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(filePath));
            }
            try
            {
                FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
                StreamWriter writer = new StreamWriter(fs, Encoding.GetEncoding("gbk"));
                foreach (string line in lines)
                {
                    try
                    {
                        writer.WriteLine(line);
                    }
                    catch (System.Exception e)
                    {
                    	
                    }
                }
                writer.Close();
                fs.Close();
            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.StackTrace);
            }
        }

        public static void WriteFile(string content, string filePath)
        {
            try
            {
                FileStream fs = new FileStream(filePath,FileMode.Create,FileAccess.Write);
                StreamWriter writer = new StreamWriter(fs, Encoding.GetEncoding("gbk"));
                writer.Write(content);
                writer.Close();
                fs.Close();
            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.StackTrace);
            }
        }


        public static void AppendLine(string content, string filePath)
        {
            try
            {
                FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write);
                StreamWriter writer = new StreamWriter(fs, Encoding.GetEncoding("gbk"));
                writer.WriteLine(content);
                writer.Close();
                fs.Close();
            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.StackTrace);
            }
        }

    }
}
